package com.shuzixiangcun.controller.admin;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.PageInfo;
import com.shuzixiangcun.entity.Users;

import com.shuzixiangcun.service.admin.UsersServiceImpl;
import com.shuzixiangcun.utils.SmsSend;
import io.swagger.models.auth.In;
import org.apache.catalina.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 用户信息表(Users)表控制层
 *
 * @author makejava
 * @since 2024-06-24 08:26:21
 */
@RestController
@CrossOrigin
@RequestMapping("admin/users")
public class UsersController {
    /**
     * 服务对象
     */
    @Resource
    private UsersServiceImpl usersService;
    @Resource
    private SmsSend smsSend;

    @PostMapping("login")
    public ResponseEntity<Users>  queryByUserNameAndPassword(@RequestParam String username,
                                                             @RequestParam String password,
                                                             HttpSession session){
        Users users=this.usersService.querybyUsernameAndPassword(username,password);
        if(users!=null){
            session.setAttribute("USER",users);
            return ResponseEntity.ok(users);
        }
        return ResponseEntity.status(500).header("Custom-Header", "foo")
                .body(this.usersService.querybyUsernameAndPassword(username,password));

    }

    @PostMapping("updateByRole")
    public ResponseEntity updateByRole(Users user){
        return ResponseEntity.ok(this.usersService.updateByRole(user));
    }

    @GetMapping("insert")
    public ResponseEntity insertUser(@RequestParam String phone,@RequestParam String password){
        Users users = new Users();
        users.setPassword(password);
        users.setPhoneNumber(phone);
        if (usersService.insertUser(users)>0){
            return ResponseEntity.status(200).header("Custom-Header", "foo")
                    .body("注册成功");
        }
        return ResponseEntity.status(500).header("Custom-Header", "foo")
                .body("注册失败");
    }

    @PostMapping("add")
    public ResponseEntity insertAUser(Users users){

        if (usersService.insertUser(users)>0){
            return ResponseEntity.status(200).header("Custom-Header", "foo")
                    .body("注册成功");
        }
        return ResponseEntity.status(500).header("Custom-Header", "foo")
                .body("注册失败");
    }

    @PostMapping("update")
    public ResponseEntity update(Users users){
        String state1=users.getState().equals("Y")?"N":"Y";

        users.setState(state1);
        if (this.usersService.update(users)>0){
            return ResponseEntity.status(200).header("Custom-Header", "foo")
                    .body("修改成功");
        }
        return ResponseEntity.status(500).header("Custom-Header", "foo")
                .body("修改失败");
    }



    @GetMapping("yzmCode")
    public ResponseEntity yzmCode(@RequestParam String phone) throws ClientException {
        Random random=new Random();
        int yzm=random.nextInt(90000) + 10000;
        Map map=new HashMap();
        String yzm1= JSON.toJSONString(yzm);
        map.put("code", yzm1);
        String params = JSON.toJSONString(map);

        smsSend.sendSms(phone,params);
        return  ResponseEntity.ok(yzm1);
    }

    @GetMapping("updateById")
    public ResponseEntity addCardIdById(Users users){
        return ResponseEntity.ok(this.usersService.addCardId(users));
    }






    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public ResponseEntity<Users> queryById(@RequestParam Integer id) {
        return ResponseEntity.ok(this.usersService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param users 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Users> add(Users users) {
        return ResponseEntity.ok(this.usersService.insert(users));
    }

    /**
     * 编辑数据
     *
     * @param users 实体
     * @return 编辑结果
     */
    @PostMapping("updateInfo")
    public ResponseEntity edit(@RequestBody Users users) {

        if (this.usersService.update(users)>0){
            return ResponseEntity.status(200).header("Custom-Header", "foo")
                    .body("修改成功");
        }
        return ResponseEntity.status(500).header("Custom-Header", "foo")
                .body("修改失败");


    }

    /**
     * 删除数据
     *
     * @param userId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.usersService.deleteById(id));
    }


    @GetMapping("/PageInfo")
    public ResponseEntity<PageInfo<Users>> getUsersList(@RequestParam Integer pageNo, @RequestParam Integer pageSize,@RequestParam(required = false) String search){

        return ResponseEntity.ok(this.usersService.getPageInfo(pageNo,pageSize,search));
    }
    }

